const articleServ = require('../services/articleService')
const { asyncHandler } = require('../routes/getSendResult')
const ArticleColumn = require('../models/index/ArticleColumn')
const columnServ = require('../services/columnService')
const moment = require('moment')
require('moment/locale/zh-cn')

// 获取文章列表
exports.getArticles = asyncHandler(async (req, res, next) => {
  const { page = 1, limit = 20 } = req.query
  const offset = (page - 1) * limit
  return await articleServ.getArticles(Number.parseInt(offset), Number.parseInt(limit))
})

// 获取单个文章
exports.getArticle = asyncHandler(async (req, res, next) => {
  const id = req.params.articleId
  const curArticle = await articleServ.getArticleById(id)
  const preArticle = await articleServ.getPreArticle(id)
  const nextArticle = await articleServ.getNextArticle(id)
  const columns = curArticle.Columns.map((item) => {
    return item.name
  })
  delete curArticle.Columns
  curArticle.dynamicColumn = columns
  return { preArticle, curArticle, nextArticle }
})

// 创建文章
exports.createArticle = asyncHandler(async (req) => {
  return await articleServ.createArticle(req.body)
})

// 更新文章
exports.updateArticle = asyncHandler(async (req) => {
  return await articleServ.updateArticle(req.params.articleId, req.body)
})

// 删除文章
exports.deleteArticle = asyncHandler(async (req) => {
  return await articleServ.deleteArticle(req.params.articleId)
})

exports.getSearchResult = asyncHandler(async (req) => {
  let { page = 1, limit = 10, value = '', year = '', month = '', type = '' } = req.query
  return await articleServ.getSearchResult(page, limit, value, year, month, type)
})

// 获取热门文章
exports.getHotArticles = asyncHandler(async (req) => {
  const { limit = 5 } = req.query
  return await articleServ.getHotArticles(limit)
})

// 计算浏览量
exports.sendRecord = asyncHandler(async (req) => {
  return await articleServ.sendRecord(req.params.articleId, req.body)
})

// 获取所有浏览量
exports.getAllRecord = asyncHandler(async () => {
  return await articleServ.getAllRecord()
})

exports.getArticleCount = asyncHandler(async () => {
  return await articleServ.getArticleCount()
})

exports.getCommentCount = asyncHandler(async () => {
  return await articleServ.getCommentCount()
})

exports.getArticleRelation = asyncHandler(async (req) => {
  return await articleServ.getArticleRelation(req.params.articleId)
})

exports.getViewsEchart = asyncHandler(async (req) => {
  const { limit = 7 } = req.query
  return await articleServ.getViewsEchart(Number.parseInt(limit))
})

exports.getSearchOption = asyncHandler(async () => {
  const result = await articleServ.getSearchOption()
  // yearOptions  [{value: '2020', label: '2020'}]
  // columnOptions [{value: column.id, label: 'column.name'}]
  const yearObj = {}

  let yearOptions = []

  let columnOptions = []

  result.forEach((item) => {
    const year = moment(item.createdAt).format('YYYY')

    if (!yearObj[year]) {
      yearObj[year] = 1
    }
  })

  const yearKey = Object.keys(yearObj)

  yearKey.forEach((item) => {
    yearOptions.push({ label: item, value: item })
  })

  // 获取专栏列表
  const columns = await columnServ.getColumns()
  columnOptions = [...columnOptions, ...columns]
  return { yearOptions, columnOptions }
})

exports.getMonth = asyncHandler(async (req) => {
  let result = await articleServ.getMonth(req.query.year)
  // monthOptions [{value: 0,label: '一月'}]
  const monthObj = {}
  let monthOptions = []
  result.forEach((item) => {
    const month = moment(item.createdAt).format('MMMM')
    if (!monthObj[month]) {
      monthObj[month] = 1
    }
  })
  const monthKey = Object.keys(monthObj)
  const monthEdit = {
    一月: 0,
    二月: 1,
    三月: 2,
    四月: 3,
    五月: 4,
    六月: 5,
    七月: 6,
    八月: 7,
    九月: 8,
    十月: 9,
    十一月: 10,
    十二月: 11
  }
  monthKey.forEach((item) => {
    monthOptions.push({ label: item, value: monthEdit[item] })
  })
  return { monthOptions }
})

// 测试
exports.createViewsEchart = asyncHandler(async (req) => {
  return await articleServ.createViewsEchart(req.body.timeName, req.body.views)
})

// 创建文章评论
exports.createArticleComment = asyncHandler(async (req, res, next) => {})

// 获取评论列表
exports.getArticleComments = asyncHandler(async (req, res, next) => {})

// 删除评论
exports.deleteArticleComment = asyncHandler(async (req, res, next) => {})
